<template>
    <div class="defaultMembership">
        <el-card>
            <el-card>
        <el-form :model="formData" :rules="formRules" ref="formRule" :loading="loading" class="formBox">
            <el-form-item>
                    <el-form-item label="选择默认隶属关系：" prop="defaultbrandcountyid">
                        <el-select placeholder="请选择隶属关系"
                                    v-model="formData.defaultbrandcountyid">
                            <el-option
                                v-for="(item,index) in fourList"
                                :key="index"
                                :label="item.companyname"
                                :value="item.companyid">
                            </el-option>
                        </el-select>
                    </el-form-item>
            </el-form-item>
            <el-form-item>
                <el-button type="primary"
                        @click="onSubmit"
                        :loading="loading">提交</el-button>
            </el-form-item>
        </el-form>
            </el-card>
        </el-card>
    </div>
</template>

<script>
import {
    getFourLevelCompanyList, // 获取4级品牌合伙公司
    getMbDefaultEntity, // 默认隶属关系详情接口
    addOrEdit // 添加/编辑默认隶属关系接口
} from "../../api/partnershipManagement/partnershipManagement.js";
export default {
    data() {
        return {
            formData: {
                defaultid: null,
                defaultbrandcountyid: null
            },
            loading: false,
            fourList: {},
            formRules: {
                defaultbrandcountyid: {
                    required: true,
                    message: "请选择隶属关系",
                    trigger: "blur"
                }
            }
        };
    },
    methods: {
        getFourLevelCompanyList() {
            getFourLevelCompanyList()
                .then(response => {
                    if (response.code) {
                        this.$message.error(response.message);
                    }
                    console.log(response);
                    this.fourList = response.data || [];
                    setTimeout(() => {
                        this.loading = false;
                    }, 0.3 * 1000);
                })
                .catch(() => {
                    this.loading = false;
                    this.fourList = [];
                });
        },
        getMbDefaultEntity() {
            this.loading = true;
            getMbDefaultEntity()
                .then(response => {
                    if (response.code) {
                        this.$message.error(response.message);
                    }
                    console.log(response);
                    this.formData.defaultbrandcountyid =
                        response.data[0].defaultbrandcountyid || null;
                    this.formData.defaultid =
                        response.data[0].defaultid || null;
                    setTimeout(() => {
                        this.loading = false;
                    }, 0.3 * 1000);
                })
                .catch(() => {
                    this.formData = [];
                    this.loading = false;
                });
        },
        onSubmit() {
            this.$refs["formRule"].validate(valid => {
                if (valid) {
                    this.loading = true;
                    addOrEdit(this.formData)
                        .then(response => {
                            if (response.code) {
                                this.loading = false;
                                this.$message.error(response.message);
                            }
                            console.log(response);
                            this.$message.success("提交成功！");
                            this.getMbDefaultEntity();
                            setTimeout(() => {
                                this.loading = false;
                            }, 0.3 * 1000);
                        })
                        .catch(() => {
                            this.formData = [];
                            this.loading = false;
                        });
                }
            });
        }
    },
    created() {
        this.getFourLevelCompanyList(); // 获取四级过列表
        this.getMbDefaultEntity(); // 获取默认隶属关系
    }
};
</script>
<style lang="scss" scoped>
.formBox {
    padding: 40px;
    margin: auto;
    .el-select {
        width: 400px;
    }
}
.defaultMembership{
    display: block;
    margin: 0 auto;
}
</style>
